package org.ascollada.core
{
	import org.ascollada.core.ns.collada;
	import org.ascollada.fx.DaeBindMaterial;
	import org.ascollada.utils.DaeLogger;
	
	public class DaeInstanceController extends DaeElement
	{
		use namespace collada;
		
		public var url :String;
		public var skeletons :Array;
		public var controller :DaeController;
		public var bindMaterial :DaeBindMaterial;
		
		/**
		 * 
		 */ 
		public function DaeInstanceController(document:DaeDocument, element:XML=null)
		{
			super(document, element);
		}

		/**
		 * 
		 */ 
		public override function read(element:XML):void
		{
			super.read(element);
	
			this.url = readAttribute(element, "url", true);
			this.skeletons = new Array();
				
			var list :XMLList = element.children();
			var i :int;
			
			for(i = 0; i < list.length(); i++)
			{
				var child :XML = list[i];
				
				switch(child.name().localName)
				{
					case "skeleton":
						var skeleton :String = readText(list[i], true);
						if(skeleton && skeleton.length)
							skeletons.push(skeleton);
						break;
					case "bind_material":
						bindMaterial = new DaeBindMaterial(document, child);
						break;
					default:
						break;
				}
			}
			
			var ctrl :XML = document.COLLADA..controller.(@id == this.url)[0];
			
			if(ctrl)
			{
				this.controller = new DaeController(document, ctrl);
			}
			else
			{
				DaeLogger.log("DaeInstanceController : could not find controller with id = " + this.url);
			}
		}
	}
}